<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: your name
// +----------------------------------------------------------------------
namespace app\WechatUser\logic;

use plugin\saiadmin\basic\BaseLogic;
use plugin\saiadmin\exception\ApiException;
use plugin\saiadmin\utils\Helper;
use app\WechatUser\model\ebWechatUser;
use plugin\saiadmin\service\OpenSpoutWriter;

/**
 * 用户列表逻辑层
 */
class ebWechatUserLogic extends BaseLogic
{
    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->model = new ebWechatUser();
    }
public function export($where = [])
{
    $query = $this->search($where);
    // ->field('id,name,enterprise_id,sort,status,create_time');
    $data = $this->getAll($query);
   

    $file_name = '用户数据.xlsx';
    // 更新表头以匹配新的列数据
    $header = ['用户信息', '手机号', '供应权重', '需求权重', '浏览权重', '收藏权重', '对接权重', '注册时间', '最后登录时间'];

    
    $filter = [
        // 'type' => [
        //     ['value' => 1, 'label' => '进行中'],
        //     ['value' => 2, 'label' => '已结束'],
        //     ['value' => 3, 'label' => '未开始']
        // ]
    ];
    
    $data_attr = [];
    foreach($data as $value){
        $data_attr[] = [
            'nickname' => $value['nickname'] ?? '',
            'phone' => $value['phone'] ?? '',
            'supply_weight' => $value['supply_weight'] ?? 0,
            'requirement_weight' => $value['requirement_weight'] ?? 0,
            'browsing_weight' => $value['browsing_weight'] ?? 0,
            'collection_weight' => $value['collection_weight'] ?? 0,
            'docking_eight' => $value['docking_eight'] ?? 0,
            'register_time' => $value['register_time'] ?? '',
            'last_login_time' => $value['last_login_time'] ?? '',
        ];
    }

    $writer = new OpenSpoutWriter($file_name);
    // 更新列宽以匹配新的列数
    $writer->setWidth([20, 20, 20, 20, 20, 20, 20, 20, 20]);


    $writer->setHeader($header);
    $writer->setData($data_attr, null, $filter);
    $file_path = $writer->returnFile();
    return response()->download($file_path, urlencode($file_name));


}

}
